<?php
//用户咨询控制器
class Controller_Admin_Chat extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type        = 'userChat';
        $this->_no_buttons       = TRUE;
        $this->_no_list_actions  = TRUE;
        $this->_list_pagesize    = 0;
        $this->_pagination_type  = 'query_string';
        $this->_pagination_param = 'page';

        parent::__construct($req, $res);

        $this->_list_where = array(
            array('messageTime', '>', 0),
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }


    public function action_chat_list($uid){
        $user = Util_User::getUserInfo($uid);
        $utype = Arr::get($user,'utype');
        if($utype == 'TOURIST'){
            list($count,$list) = User_Chat::getTouristChatByUidAdmin($uid);
        }else if($utype == 'GUIDER'){
            list($count,$list) = User_Chat::getGuiderChatByUidAdmin($uid);
        }else{
            return ;
        }
//        var_dump($list);
//        $user = User::getUserFromUid($uid);
//        $chatList = App_Chat::infoList($user,"","","",1);
        $this->template->set_filename('smarty:admin/chat/chat_list');
        $this->template->set('chatList', $list);
        $this->template->set('chatUid', $uid);
//        var_dump($user);
    }



	//用户咨询页
	//$uid int 用户id
    public function action_user ($uid)
    {
        $query = $this->request->query();
        $id = Arr::get($query,'id');
        $user_v2 = User::getUserFromUid($uid);
        $user_v2_1 = User::getUserFromUid($id);
//
//        $chatList = App_Chat::infoList($user,"","","",1);
//        var_dump($chatList);
        $user = Util_User::getUserInfo($id);
        $utype = Arr::get($user,'utype');
        if($utype == 'TOURIST'){
            $chatList = App_Chat::infoListAdmin($user_v2_1,$uid);
        }else if($utype == 'GUIDER'){
            $chatList = App_Chat::guiderInfoListAdmin($user_v2,$id);
        }else{
            return ;
        }

//       var_dump($chatList);
        $name1 = $user_v2_1->getUserName();
        $name2 = $user_v2->getUserName();

        $this->template->set_filename('smarty:admin/chat/chat');
        $this->template->set('chatList', $chatList);
        $this->template->set('name1', $name1);
        $this->template->set('name2', $name2);
        $this->template->set('chatList', $chatList);
        $this->template->set('chatUid', $uid);
    }

    public function action_refresh($uid)
    {
        $user     = User::getUserFromUid($uid);
        $chatList = App_Chat::infoList($user);
        $html     = View::factory('smarty:admin/chat/records')
            ->set('chatList', $chatList)
            ->render();

        $ret = array('status' => 0, 'html' => $html);
        echo json_encode($ret);exit;
    }



	//最新消息
    public function action_newest ()
    {
        $this->_no_list_actions  = FALSE;
        $page = $this->request->query('page');
        $page = max(1, $page);
        parent::action_list($page);
    }

    public function list_get_data ($offset, $limit)
    {
        $list = App_Account::newChatMessages($offset, $limit);
        return $list;
    }

    public function actions_by_list_row (& $row, & $menus)
    {
        array_pop($menus);
        array_pop($menus);
        array_pop($menus);

        $menus[] = '<a href="/admin/chat/user/'.$row['uid'].'" target="_blank">进入聊天</a>';
    }

	//接受并处理提交到服务器的信息
    public function action_api ()
    {
        $this->_post = $this->request->post();
        $img = Arr::get($this->_post, 'img');
        $txt = Arr::get($this->_post, 'txt');
        $toUid = Arr::get($this->_post, 'touid');

        if (! empty($img))
            $img = "<img src=\"$img\">";

        if (! empty($txt))
        {
            $txt = htmlentities($txt);
            $txt = nl2br($txt);
        }

        if (empty($txt) && empty($img))
            exit;

        $txt   = empty($img) ? $txt : $img;
        if ($toUid > 0)
        {
            $user  = User::getUserFromUid($toUid);
            $msgId = App_Chat::add($user, array('message' => $txt, 'fromUser' => 0));
        }
        else
        {
            $msgId = App_Chat::add($this->_user, array('message' => $txt, 'fromUser' => 1));
        }

        $html  = App_Chat::buildMessageHtml($msgId);
            
        echo json_encode(array(
            'status' => 0,
            'html'   => $html,
        ));

        exit;
    }
}
